<?php
/**
 * Created by sublime
 * User: 姚鹏
 * Date: 2017-11-01
 * Time: 15:51
 */
namespace app\mall\controller;
use controller\BasicAdmin;
use service\DataService;
use think\Db;
use app\comm\service\AdminUserService;
use app\comm\service\SysToolsLevePoint;
use app\comm\service\GoodsService;
use app\mall\model\Coupon;
class Activity extends BasicAdmin
{
	/**
	 * 优惠券维护
	 * @return [type] [description]
	 */
	public function index(){
		$this->title = '优惠券活动';
        $urlend = $this->request->get('', '', 'urlencode');
      	$get = $this->request->get();
      	$join = [
      		['ys_goods b','a.goods_id=b.id','LEFT'],
      		['ys_store c','a.store_id=c.id','LEFT']
      	];
      	$db = Db::name('coupon')->alias('a')->join($join)->field('a.*,b.name as goodsname,c.title');
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike('a.name', "%{$get[$key]}%");
        }
        $yangshi = config("qiniu");
        $this->assign('yangshi',$yangshi['yangshi']);          //七牛样式后缀
      	return parent::_list($db);
	}	

	/**
	 * 编辑 优惠券
	 */
	public function checklook($id){
		$systoolspoint = new SysToolsLevePoint;
		if (request()->isPost()){
			$coup = $systoolspoint->setbycoupidustaus($id);
			$this->success('恭喜,已经设为无效!','');
		}

		if (request()->isGet()){
			$coup = $systoolspoint->getbycoupid($id);
			$g_name = Db::name('goods')->where('id',$coup['goods_id'])->value('name');
			$this->assign('goods',$g_name);
			return $this->fetch('', ['coup'=>$coup]);	
		}
		
	}

	/**
	 * 设置状态
	 */
	public function  setsatus($id){
		$systoolspoint = new SysToolsLevePoint;
		$coup = $systoolspoint->setbycoupidustausswitch($id);
		$this->success('恭喜,设置成功!','');
	}

	/**
	 * 添加 优惠券
	 */
	public function addcoupon(){
		$this->title = '优惠券添加';
		$systoolspoint  = new SysToolsLevePoint();
		if (request()->isPost()){
			$data['send_start_time'] ="发放时间";
		    $data['send_end_time']   ='结束时间';
		    $data['use_start_time']  ='使用开始时间';
		    $data['use_end_time']    ='使用结束时间';
		    $post = $this->request -> post();
		    foreach ($data as $key => $value) {
		    	if(empty($post[$key])){
		    		$this->error('请填'.$value, '');
		    	}
		    }
			$sle = $systoolspoint  -> addcoupon($post);
			$this->success('恭喜, 数据保存成功!','');
		}
		$list = (new GoodsService())->getgoodslistall();
		return $this->fetch('', ['list'=>$list]);
	}
	/**
	 * 删除
	 * @Author   lingyun
	 * @DateTime 2017-11-28T19:39:34+0800
	 * @return   [type]                   [description]
	 */
	public function del(){
		$id = input('id');		//优惠券的ID
		$res = Db::name('coupon')->where('id',$id)->delete();
        $res>0?$this->success('删除成功',''):$this->error("删除失败，请稍候再试！");
	}		
}
?>